"""
公共方法
"""

import os
import logging
import hashlib


def pwd_to_sha256(pwd):
    sha = hashlib.sha256()
    sha.update(pwd.encode("utf-8"))
    # 加盐
    sha.update("salt".encode("utf-8"))
    return sha.hexdigest()  # 返回sha256加密后的密码


def get_logger(log_name):

    logger = logging.getLogger(log_name)  # 获取logger实例
    logger.setLevel(logging.DEBUG)  # 设置日志级别

    # 设置日志格式
    formatter = logging.Formatter(
        "%(asctime)s - %(name)s - %(levelname)s - %(message)s",
        datefmt="%Y-%m-%d %H:%M:%S",
    )

    ch = logging.StreamHandler()  # 输出到控制台
    ch.setLevel(logging.DEBUG)  # 输出级别
    ch.setFormatter(formatter)  # 输出格式

    logger.addHandler(ch)  # 添加输出处理器

    # 设置log信息存放路径
    base_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "logs\\")
    logs_path = f"{base_path}{log_name}.log"
    fh = logging.FileHandler(logs_path)  # 输出到文件
    fh.setFormatter(formatter)  # 输出格式
    logger.addHandler(fh)  # 添加输出处理器

    return logger


if __name__ == "__main__":

    abc = "test"
    base_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "logs\\")
    logs_path = f"{base_path}{abc}.log"
    print(logs_path)
